package com.honey.comment.grammer.colon;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.honey.comment.grammer.DatabaseCommentGrammar;
import com.honey.core.dbmapping.structure.ColumnValidator;
import com.honey.core.dbmapping.structure.EnumType;
import com.honey.general.databases.introspected.GeneralColumnValidator;
import com.honey.general.databases.introspected.GeneralEnum;

public class ColonCommentGrammar implements DatabaseCommentGrammar {

	private MatchItem matchEntity = null;
	
	private MatchItem matchVirtualDelete = null;
	
	
	private MatchItem matchValidator = null;
	
	private MatchItem matchComment = null;
	
	private GeneralEnum generalEnum = null ;
	
	private List<ColumnValidator> validator = null;
	
	public ColonCommentGrammar(){
		matchEntity = new MatchEntityName();
		matchVirtualDelete = new MatchVirtualDelete();
		matchValidator = new MatchValidator();
		matchComment = new MatchComment();
	}
	
	@Override
	public String getComment() {
		return matchComment.getValue();
	}

	@Override
	public String getEntityName() {
		return matchEntity!= null ?matchEntity.getValue():null ;
	}

	@Override
	public EnumType getEnumType() {
		return generalEnum;
	}

	@Override
	public String getVirtualDelete() {
		
		return matchVirtualDelete.getValue();
	}

	@Override
	public ColumnValidator[] getValidator() {
		
		return validator==null ?
					null
					:
					(
						validator.size()>0?
									(ColumnValidator[])validator.toArray(
											new GeneralColumnValidator[validator.size()]
											)
									:
									null 
					);
	}

//	@Override
//	public VirtualForeignKey getVirtualForeignKey() {
//		
//		return null;
//	}
	
	private static String regex = "\\s";
	
	private static Pattern pattern = Pattern.compile(regex,Pattern.DOTALL | Pattern.CASE_INSENSITIVE);
	
	@Override
	public void parser(String comment) {
		Matcher matcher = pattern.matcher(comment);
		int start =0;
		String item = null;
		while(matcher.find()){
			item = comment.substring(start, matcher.start() ) ;
			start = matcher.end();
			if(item.length()>0) match(item);
		}
		if( start < comment.length() ){
			item = comment.substring(start, comment.length() ) ;
			if(item.length()>0) match(item);
		}
	}

	private void match(String item ){
		//实体
		if( matchEntity.match(item)) return ;
		
		//虚拟删除
		if( matchVirtualDelete.match(item)) return ;
		
		//效验
		if( matchValidator(item)) return ;
		
		//放在后面检查
		if(matchEnum(item)) return ;
		
		//注释必须放在最后
		if( matchComment.match(item)) return ;
	}
	//
	
	private boolean matchValidator(String item ){
		boolean answer = false;
		answer = matchValidator.match(item);
		if( answer ){
			if(validator == null){
				validator = new ArrayList<ColumnValidator>();
			}
			GeneralColumnValidator v = new GeneralColumnValidator();
			v.setValidatorName(matchValidator.getValue());
			validator.add( v );
		}
		return answer;
	}
	
	private boolean matchEnum(String item ){
		boolean answer = false;
		MatchEnumValue ev = new MatchEnumValue();
		answer = ev.match(item) ;
		if( answer ){

			if(generalEnum == null){
				generalEnum = new GeneralEnum();
			}
			String enumField = ev.getEnumField();
			String javaProperty =enumField.toUpperCase();
			char s = enumField.charAt(0);
			if( s>=0x30 && s<= 0x39 ){
				javaProperty ="ENUM_"+javaProperty;
			}
			generalEnum.addEnumValue(ev.getEnumValue(),ev.getEnumField(),javaProperty,ev.getEnumFieldComment());
		}
		return answer;
	}
}
